<template>
  <div>
    <van-dropdown-menu>
      <van-dropdown-item title="综合" disabled />
      <van-dropdown-item
        title="价格"
        v-model="priceVal"
        :options="option2"
        @change="priceChange"
      />
      <van-dropdown-item
        title="分类"
        v-model="categoryVal"
        :options="filterCategory"
        @change="categoryChange"
      />
    </van-dropdown-menu>

    <van-empty
      v-if="goodsList.length == 0"
      image="search"
      description="没有您搜索的商品"
    />
    <Products v-else :goodsList="goodsList" />
  </div>
</template>

<script>
import Products from "./Products";

export default {
  props: ["goodsList", "filterCategory"],
  data() {
    return {
      priceVal: "",
      categoryVal: 1005000, //这个的值是哪一项的value，就表示这一项被勾上
      option2: [
        { text: "价格由低到高", value: "asc" },
        { text: "价格由高到低", value: "desc" }
      ]
    };
  },
  beforeUpdate() {
    // 数据挂载完毕 遍历filterCategory数组，看看哪一项的checked为true，
    //遍历的时候遇到checked为true时，就把 this.categoryVal改成它的value值 这样这一项就被勾上了
    this.filterCategory.map(item => {
      if (item.checked) {
        this.categoryVal = item.value;
        return;
      }
    });
  },
  components: {
    Products
  },
  methods: {
    categoryChange(val) {
      // console.log("**************", val); //就是分类id
      this.$emit("categoryChange", val);
    },
    priceChange(value) {
      console.log(value);
      this.$emit("priceChange", value);
    }
  }
};
</script>
 
<style lang = "less" scoped>
</style>